package com.atguigu.eduorder.controller;


import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.R;
import com.atguigu.eduorder.entity.Order;
import com.atguigu.eduorder.service.OrderService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author dujiajia
 * @since 2020-11-07
 */
@RestController
@RequestMapping("/eduorder/order")
@CrossOrigin
@Api(tags = "订单管理")
public class OrderController {

    @Autowired
    private OrderService orderService;



    @ApiOperation("生成订单的方法")
    @PostMapping("createOrder/{courseId}/{memberId}")
    public R createOrder(@PathVariable String courseId, @PathVariable String memberId) {
        String orderNo = orderService.createOrders(courseId,memberId);
        return R.ok().data("orderId",orderNo);
    }

    @ApiOperation("根据订单号查询订单信息")
    @GetMapping("getOrderInfo/{orderNo}")
    public R getOrderInfo(@PathVariable("orderNo") String orderNo){
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("order_no",orderNo);
        Order order = orderService.getOne(wrapper);
        return R.ok().data("order",order);
    }

    @ApiOperation("根据课程id和用户id查询 课程是否被购买")
    @GetMapping("isBuyCourse")
    public boolean isBuyCourse(@RequestParam("courseId") String courseId,@RequestParam("memberId") String memberId) {
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        wrapper.eq("member_id",memberId);
        wrapper.eq("status",1);

        int count = orderService.count(wrapper);
        if (count > 0) {
            return true;
        }else{
            return false;
        }
    }

}

